package com.cqc.googleplay.activity;

import com.cqc.googleplay.R;
import com.cqc.googleplay.bean.AppInfoBean;
import com.cqc.googleplay.http.HttpHelper;
import com.cqc.googleplay.http.NetUrl;
import com.cqc.googleplay.utils.CommonUtil;
import com.cqc.googleplay.utils.GsonUtil;
import com.cqc.googleplay.view.ContentPager;
import com.cqc.googleplay.view.part.AppInfoDesPart;
import com.cqc.googleplay.view.part.AppInfoPart;
import com.cqc.googleplay.view.part.AppInfoScreenPart;
import com.cqc.googleplay.view.part.AppSafePart;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class AppDetailActivity extends ActionBarActivity {
	
	private ContentPager contentPager;
	private Context context;
	private View rootView;
	private ScrollView scrollvi1ew;
	private LinearLayout layout_parts;
	private LinearLayout layout_bottom;
	private String packageName;
	private AppInfoBean infoBean;
	private AppInfoPart part1;
	private AppSafePart part2;
	private AppInfoScreenPart part3;
	private AppInfoDesPart part4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		contentPager = new ContentPager(this) {
			
			@Override
			public Object requestData() {
				return getData();
			}
			
			@Override
			public View createSuccessView() {
				return getSuccessView();
			}
		};
		setContentView(contentPager);
		initActionBar();		
	}

	protected Object getData() {
		packageName = getIntent().getStringExtra("packageName");
		String json = HttpHelper.get(NetUrl.DETAIL+packageName);
		infoBean = GsonUtil.jsonToBean(json, AppInfoBean.class);
		//赋值
		if (infoBean != null ) {
			CommonUtil.runOnUIThread(new Runnable() {
				
				@Override
				public void run() {
					setAllPartsData(infoBean);
				}
			});
		}
		return json;
	}

	protected void setAllPartsData(AppInfoBean infoBean) {
		if (part1 != null) {
			part1.setData(infoBean);
		}
		if (part2 != null) {
			part2.setData(infoBean);
		}		
		if (part3 != null) {
			part3.setData(infoBean);
		}		
		if (part4 != null) {
			part4.setData(infoBean);
		}		
	}

	protected View getSuccessView() {
		rootView = View.inflate(context, R.layout.activity_detail, null);
		scrollvi1ew = (ScrollView) rootView.findViewById(R.id.scrollView_detail_activity);//id为scrollView 的话，获取的是pulltorefresh的id
		layout_parts = (LinearLayout) rootView.findViewById(R.id.layout_parts);
		layout_bottom = (LinearLayout) rootView.findViewById(R.id.layout_bottom);
		
		part1 = new AppInfoPart();
		layout_parts.addView(part1.getView());
		
		part2 = new  AppSafePart();
		layout_parts.addView(part2.getView());
		
		part3 = new AppInfoScreenPart();
		layout_parts.addView(part3.getView());
		
		part4 = new AppInfoDesPart();
		layout_parts.addView(part4.getView(scrollvi1ew));
		return rootView;
	}

	private void initActionBar() {
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayShowHomeEnabled(true);
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setTitle("应用详情");	
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		//点击导航栏，让其消失
		if (item.getItemId() == android.R.id.home) {
			finish();
		}
		return super.onOptionsItemSelected(item);
	}
}
